package com.bw.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.bw.entity.Dict;
import com.bw.entity.JobInfo;
import com.bw.entity.JobQuery;
import com.bw.mapper.JobInfoMapper;
import com.bw.service.JobInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Select;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Controller
public class JobController {


    @Autowired
    JobInfoMapper jobInfoMapper;
    @Autowired
    JobInfoService jobInfoService;

    @RequestMapping("/list.do")
    public String list(JobQuery query, Integer pageNum, Model model){

        //1. 下拉框
        selectData(model);

        // 2. 分页查询
        PageHelper.startPage(pageNum == null ? 1:pageNum,3);
        List<JobInfo>  list = jobInfoMapper.selectByParam(query);
        PageInfo<JobInfo> pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);



        // 3. 条件回显
        model.addAttribute("query",query);
        return "list";
    }


    @RequestMapping("/del.do")
    public String del(String ids){
        System.out.println("要删除的id:" + ids);
        jobInfoMapper.delById(ids);

        return "redirect:list.do";
    }


    @RequestMapping("/addPage.do")
    public String addPage(Model model){
        //1. 下拉框
        selectData(model);
        return "add";
    }

    @RequestMapping("/add.do")
    public String add(@Valid JobInfo addJob, BindingResult result,Model model){
        // 1. 非空验证
        if (result.hasErrors()){

            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            //1. 下拉框
            selectData(model);
            return "add";
        }

        //2. 员工编号以“GH”开始
        String num = addJob.getNum();
        if (!num.startsWith("GH")){
            model.addAttribute("error","员工编号以“GH”开始");
            //1. 下拉框
            selectData(model);
            return "add";
        }
        //3. 开始时间小于结束时间
        String startTimeStr = addJob.getStartTime();
        DateTime startTime = DateUtil.parse(startTimeStr, "yyyy-MM-dd");
        String endTimeStr = addJob.getEndTime();
        DateTime endTime = DateUtil.parse(endTimeStr, "yyyy-MM-dd");

        if (startTime.isAfter(endTime)){
            model.addAttribute("error","开始时间小于结束时间");
            //1. 下拉框
            selectData(model);
            return "add";
        }

        //执行添加  为了事务需要调用service
        jobInfoService.addJob(addJob);
        return "redirect:list.do";
    }


    private void selectData(Model model){
        //1. 下拉框
        List<Dict>  postList = jobInfoMapper.listPost();
        model.addAttribute("postList",postList);
        List<Dict> schoolList = jobInfoMapper.listSchool();
        model.addAttribute("schoolList",schoolList);
    }
}
